package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;


/**
 * @Program: health_parent
 * @Description:
 * @Author: choulan
 * @Date:2020-11-27 16:23
 **/
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;
    //分页查询
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return menuService.findPage(queryPageBean);
    }


    //查找所有的菜单:  data:[{id:1,name:''},{id:2,name:''}]
    @RequestMapping("/findParentMenuIds")
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public Result findParentMenuIds(){
        try {
            List<Menu> list= menuService.findParentMenuIds();
            return new Result(true,MessageConstant.GET_MENUS_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENUS_FAIL);
        }
    }

    @RequestMapping("/checkName")
    public Result checkName(@RequestBody Map jsonData){
        try {
            String name = (String)jsonData.get("name");
            boolean isExsist= menuService.checkName(name);
            return new Result(true,"校验成功",isExsist);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"校验失败");
        }
    }

    @PreAuthorize("hasAuthority('MENU_DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            menuService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MENU_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
    }

    @PreAuthorize("hasAuthority('MENU_QUERY')")
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Menu menu= menuService.findById(id);
            return new Result(true,MessageConstant.GET_MENU_SUCCESS,menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }
    }

    @PreAuthorize("hasAuthority('MENU_QUERY')")
    @RequestMapping("/findParentMenuId")
    public Result findParentMenuId(Integer childrenId){
        try {
            String menuName= menuService.findParentMenuId(childrenId);
            return new Result(true,MessageConstant.GET_MENU_SUCCESS,menuName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }
    }
    @PreAuthorize("hasAuthority('MENU_ADD')")
    @RequestMapping("/add")
    public Result add(String name, @RequestBody Menu menu){
        try {
            if(name!=null&&!"".equals(name)){
                Integer parentId= Integer.parseInt(name);
                menu.setParentMenuId(parentId);
            }
            menuService.add(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MENU_FALL);
        }
        return new Result(true,MessageConstant.ADD_MENU_SUCCESS);
    }
    @PreAuthorize("hasAuthority('MENU_EDIT')")
    @RequestMapping("/edit")
    public Result edit(String name, @RequestBody Menu menu){
        try {
            Integer parentId= Integer.parseInt(name);
            menu.setParentMenuId(parentId);
            menuService.edit(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MENU_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_MENU_SUCCESS);
    }



}
